package com.bonc.rms.controller.tms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.service.impl.tms.EmployeeServiceimpl;
import com.bonc.rms.service.tms.EmployeeRRService;
import com.bonc.rms.utils.impl.EmployeeRRUtilImpl;

@Controller
@RequestMapping(value = "/employeeRRController")
public class EmployeeRRController {

	@Autowired
	EmployeeRRService employeeRRService;
	@Autowired
	EmployeeServiceimpl employeeService;
	
	
	/**
	 * 
	 * @param departmentId
	 * @return 员工增长率
	 */
	@RequestMapping(value = "/employeeGrowth")
	@ResponseBody
	public String getEmployeeGrowth(Integer departmentId, String startTime,
			String endTime) {
		LogManager.Method("Int  Service : EmployeeRRController -> getEmployeeGrowth");
		Map<String, Object> entryLeaveMap = new HashMap<String, Object>();
		List<String> titleList = new ArrayList<String>();
		titleList.add("员工增长率");
		entryLeaveMap.put("title", titleList);
		entryLeaveMap.put("name1", "比率");
		
		List<String> andRateList =new ArrayList<>();
		 andRateList = employeeRRService.getEmployeeGrowth(
				departmentId, startTime, endTime);
		Map<String, Object> andRateMap = new HashMap<String, Object>();
		andRateMap.put("name", "员工增长率");
		andRateMap.put("type", "line");
		andRateMap.put("data", andRateList.toArray());
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataList.add(andRateMap);

		entryLeaveMap.put("data", dataList);

		if ((startTime == null || startTime.equals(""))
				& (endTime == null || endTime.equals(""))) {

			List<String> datesList = new ArrayList<String>();
			Date date = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String year = formatter.format(date).substring(0, 4);
			datesList.add(year + "年1月");
			datesList.add(year + "年2月");
			datesList.add(year + "年3月");
			datesList.add(year + "年4月");
			datesList.add(year + "年5月");
			datesList.add(year + "年6月");
			datesList.add(year + "年7月");
			datesList.add(year + "年8月");
			datesList.add(year + "年9月");
			datesList.add(year + "年10月");
			datesList.add(year + "年11月");
			datesList.add(year + "年12月");
			entryLeaveMap.put("dates", datesList);
		} else {

			entryLeaveMap.put("dates", getTimeSpan(startTime, endTime));
		}
		LogManager.Method("Out  Service : EmployeeRRController -> getEmployeeGrowth");
		return JSON.toJSONString(entryLeaveMap);

	}

	/**
	 * 获取跨区时间年月份工具方法
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<String> getTimeSpan(String startTime, String endTime) {
		LogManager.Method("Int  Service : EmployeeRRController -> getTimeSpan");
		String startYear = startTime.substring(0, 4);
		String start = (startTime.substring(5, 6)).equals("0") ? startTime
				.substring(6, 7) : startTime.substring(5, 7);
		int startT = Integer.parseInt(start);
		int startYearT = Integer.parseInt(startYear);

		String end = (endTime.substring(5, 6)).equals("0") ? endTime.substring(
				6, 7) : endTime.substring(5, 7);
		String endYear = endTime.substring(0, 4);
		int endT = Integer.parseInt(end);
		int endYearT = Integer.parseInt(endYear);

		List<String> datesList = new ArrayList<String>();

		if (startYearT == endYearT) {
			for (int i = startT; i <= endT; i++) {

				datesList.add(startYearT + "年" + i + "月");

			}
		} else {
			for (int i = startT; i <= 12; i++) {

				datesList.add(startYearT + "年" + i + "月");
			}
			int y = endYearT - startYearT;

			for (int i = 1; i < y; i++) {

				int year = startYearT + i;
				for (int j = 1; j < 13; j++) {

					datesList.add(year + "年" + j + "月");
				}

			}

			for (int i = 1; i <= endT; i++) {

				datesList.add(endYearT + "年" + i + "月");
			}
		}
		LogManager.Method("Out  Service : EmployeeRRController -> getTimeSpan");
		return datesList;
	}

	/**
	 * 
	 * @param departmentId
	 * @param startTime
	 *            时间跨度
	 * @param endTime
	 * @return 总工资增长率
	 */
	@RequestMapping(value = "/salaryGrowt")
	@ResponseBody
	public String getSalaryGrowt(Integer departmentId, String startTime,
			String endTime) {
		LogManager.Method("Int  Service : EmployeeRRController -> getSalaryGrowt");
		Map<String, Object> entrySalaryMap = new HashMap<String, Object>();

		List<String> titleList = new ArrayList<String>();
		titleList.add("年度总额");
		titleList.add("增长率");
		entrySalaryMap.put("title", titleList);

		List<String> salaryList = employeeRRService.getSalaryGrowt(
				departmentId, startTime, endTime);
		Map<String, Object> salaryMap = new HashMap<String, Object>();
		salaryMap.put("name", "年度总额");
		salaryMap.put("type", "bar");
		salaryMap.put("barWidth", "50");
		salaryMap.put("data", salaryList.toArray());
		
		List<String> rateList = EmployeeRRUtilImpl.getSalaryGrowRrate(salaryList);
		Map<String, Object> rateMap = new HashMap<String, Object>();
		rateMap.put("name", "增长率");
		rateMap.put("type", "line");
		rateMap.put("yAxisIndex", "1");
		rateMap.put("data", rateList.toArray());
		
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		
		dataList.add(salaryMap);
		dataList.add(rateMap);
		
		entrySalaryMap.put("data", dataList);

		List<String> datesList = new ArrayList<String>();
		if ((startTime == null || startTime.equals(""))
				& (endTime == null || endTime.equals(""))) {

			Date date = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String year = formatter.format(date).substring(0, 4);
			int yearInt = Integer.parseInt(year);
			int lastYearInt = yearInt - 1;
			int lastYearInt2 = lastYearInt - 1;
			datesList.add(lastYearInt2 + "年");
			datesList.add(lastYearInt + "年");
			datesList.add(yearInt + "年");
			entrySalaryMap.put("dates", datesList);
		} else {
			
			String startYear = startTime.substring(0, 4);
			String endYear = endTime.substring(0, 4);
			int startYearInt = Integer.parseInt(startYear);
			int endYearInt = Integer.parseInt(endYear);
			
			for (int i = startYearInt; i <= endYearInt; i++) {
				datesList.add(i+"年");
			}
			
			entrySalaryMap.put("dates", datesList);
			

		}
		
		entrySalaryMap.put("name1", "年度总额");
		entrySalaryMap.put("name2", "增长率");
		LogManager.Method("Out Service : EmployeeRRController -> getSalaryGrowt");
		return JSON.toJSONString(entrySalaryMap);
	}
}